package logic

import (
	"context"
	"fmt"
	"shop/model/mysql"

	"shop/shoprpc/internal/svc"
	"shop/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *shoprpc.LoginRequest) (*shoprpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	data, _ := user.FindUser(mysql.Db, in.Account)
	if data.ID == 0 {
		m := mysql.User{
			Account:  in.Account,
			Password: in.Password,
		}
		mysql.Db.Debug().Create(&m)
	} else {
		if in.Password != user.Password {
			return nil, fmt.Errorf("账号密码输入错误")
		}
	}

	return &shoprpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
